home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d974.lha / DDLI / qst.c < prev    next >
C/C++ Source or Header  |  1994-01-04  |  4KB  |  190 lines

  1. /* QST.C  Copyright (C) 1993 Fergus Patrick Duniho */
  2.  
  3. #define MAXLINE 256
  4.  
  5. typedef struct qnode {
  6.     int p[2]; /* Preference Array Numbers */
  7.     int num, ans, strength;
  8.     char *Qtext, *Atext[2];
  9.     struct qnode *pv, *nx;
  10. } qst;
  11.  
  12. qst *RND_QList (qst *Q, int size);
  13. char *Get_QList (char *fname, qst *Q, int size);
  14. void AskQuestions (qst *Q, int col);
  15. int AskQ (qst *Q, int col);
  16. char get_answer(int col);
  17. qst *new_qnode (int num);
  18. int get_strength (int col);
  19.  
  20. qst *new_qnode (int num) {
  21.     qst *node;
  22.  
  23.     if ((node = (qst *)malloc(sizeof(qst))) == NULL) {
  24.       perror ("Malloc can't allocate enough memory for a new qnode.\n");
  25.       exit (2);
  26.    }
  27.    node->ans = -1;
  28.    node->num = num;
  29.    return node;
  30. }
  31.  
  32. /* Get_QList: Reads questions from a file into a linked list.
  33.    Returns the version # string from the beginning of the
  34.    questions file. */
  35.  
  36. char *Get_QList (char *fname, qst *Q, int size) {
  37.     FILE *fptr;
  38.     int i, j;
  39.     char *V;
  40.     qst *S;
  41.  
  42.     S = Q;
  43.     RdOpen (fptr, fname);
  44.     V = clone_line(fptr, MAXLINE);
  45.     for (i = 1; i <= size; i++) {
  46.         Q->nx = new_qnode(i);
  47.         Q->nx->pv = Q;
  48.         Q = Q->nx;
  49.         Q->Qtext = clone_line(fptr, MAXLINE);
  50.         for (j = 0; j < 2; j++) {
  51.             Q->p[j] = fgetp(fptr);
  52.             Q->Atext[j] = clone_line(fptr, MAXLINE);
  53.         }
  54.     }
  55.     fclose (fptr);
  56.     Q->nx = S;
  57.     return V;
  58. }
  59.  
  60. /* nth_qnode: Returns the nth member of a linked list of qst nodes. */
  61.  
  62. qst *nth_qnode (qst *Q, unsigned int n) {
  63.     while (n--)
  64.         Q = Q->nx;
  65.     return Q;
  66. }
  67.  
  68. /* RND_QList: Randomizes a linked list of questions. */
  69.  
  70. qst *RND_QList (qst *Q, int size) {
  71.     qst *S, *L, *N, *SN;
  72.     unsigned int i, j;
  73.     char *temp;
  74.  
  75.     SN = N = new_qnode(0);
  76.     S = Q;
  77.  
  78.     /* Randomizes Answer Order */
  79.     for (i = 0; i < size; i++)
  80.         if (rand() % 2) {
  81.             L = nth_qnode(S, i);
  82.             j = L->p[1]; L->p[1] = L->p[0]; L->p[0] = j;
  83.             temp = L->Atext[1];
  84.             L->Atext[1] = L->Atext[0];
  85.             L->Atext[0] = temp;
  86.         }
  87.  
  88.     /* Randomizes Question Order */
  89.     for (; size; size--) {
  90.         i = (rand() % size) + 1;
  91.         L = nth_qnode(S, i);
  92.         L->nx->pv = L->pv;
  93.         L->pv->nx = L->nx;
  94.         N->nx = L;
  95.         L->pv = N;
  96.         N = L;
  97.     }
  98.     free (S);
  99.     N->nx = SN;
  100.     SN->pv = N;
  101.     return SN;
  102. }
  103.  
  104. void AskQuestions (qst *Q, int col) {
  105.     int more = 1, flag = 1, num;
  106.  
  107.     while (more) {
  108.         for (Q = Q->nx, num = 1; Q->num > 0; Q = Q->nx, num++) {
  109.             if ((Q->ans != -1) && flag)
  110.                 continue;
  111.             flag = 1;
  112.             printf ("\nQuestion #%d\n", num);
  113.             if (AskQ(Q, col)) {
  114.                 Q = Q->pv->pv;
  115.                 flag = 0;
  116.                 num -= 2;
  117.                 if (num < 0) num = 0;
  118.             }
  119.         }
  120.         more = 0;
  121.         for (Q = Q->nx; Q->num > 0; Q = Q->nx)
  122.             if (Q->ans == -1) more = 1;
  123.     }
  124. }
  125.  
  126. /* AskQ: Asks a single question. */
  127.  
  128. int AskQ (qst *Q, int col) {
  129.     char A;
  130.  
  131.     wrapwrite (stdout, 0, 0, col, Q->Qtext, 0);
  132.     wrapwrite (stdout, 0, 4, col, "(A)", Q->Atext[0], 0);
  133.     puts (" ... Or");
  134.     wrapwrite (stdout, 0, 4, col, "(B)", Q->Atext[1], 0);
  135.     if ((A = get_answer(col)) < 2) {
  136.         Q->ans = Q->p[A];
  137.         Q->strength = get_strength(col);
  138.     }
  139.     else if (A == 4)
  140.         exit (2);
  141.     return A == 3;
  142. }
  143.  
  144. char get_answer(int col) {
  145.     char c;
  146.  
  147.     for (;;) {
  148.         c = fgetc(stdin);
  149.         while (fgetc(stdin) != '\n');
  150.         if (islower(c))
  151.             c = toupper(c);
  152.         if (c == 'A')
  153.             return 0;
  154.         if (c == 'B')
  155.             return 1;
  156.         if (c == 'S')
  157.             return 2;
  158.         if (c == 'P')
  159.             return 3;
  160.         if (c == 'Q')
  161.             return 4;
  162.         wrapwrite (stdout, 0, 0, col,
  163.             "Answer with an A or a B.\n",
  164.             "P = Go back to Previous question\n",
  165.             "Q = Quit\n",
  166.             "S = Skip until later.\n", 0);
  167.     }
  168. }
  169.  
  170. int get_strength (int col) {
  171.     int i;
  172.     wrapwrite (stdout, 0, 0, col, "Please rate the strength of this",
  173.         "preference on a scale of 1 to 7.\n",
  174.         "1 = Faint\n",
  175.         "2 = Weak\n",
  176.         "3 = Modest\n",
  177.         "4 = Moderate\n",
  178.         "5 = Strong\n",
  179.         "6 = Extreme\n",
  180.         "7 = Excessive\n", 0);
  181.     for (;;) {
  182.         i = fgetp(stdin);
  183.         if (i < 8)
  184.             break;
  185.         wrapwrite (stdout, 0, 0, col,
  186.             "Please select a number from 1 to 7.", 0);
  187.     }
  188.     return i;
  189. }
  190.